from openpyxl.reader.excel import load_workbook
from docx import Document
from docx.enum.style import WD_STYLE_TYPE, WD_BUILTIN_STYLE
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_TAB_LEADER
from docx.oxml.ns import qn
from docx.shared import Pt, Inches, RGBColor
from gtj.i_use.i_use import get_excel_names
from gtj.word.number_mapping import get_china_number, get_roman_number
from gtj.word.roster_word_paragraph_method import add_paragraph_heading1, add_paragraph_heading2, \
    add_paragraph_heading3, add_paragraph_heading4, add_paragraph_heading5

print('111')
source_path = r'D:\Desktop\word\source'

file_names = get_excel_names(r'D:\Desktop\word\source')
print(file_names)


source_excel = load_workbook(f'{source_path}\{file_names[0]}')


source_sheet = source_excel['1级']
sort1 = []
for row in source_sheet.iter_rows():
    sort1.append(row[0].value)

sort2 = []
source_sheet = source_excel['2级']
for row in source_sheet.iter_rows():
    sort2.append(row[0].value)

sort3 = []
source_sheet = source_excel['3级']
for row in source_sheet.iter_rows():
    sort3.append(row[0].value)

sort4 = []
source_sheet = source_excel['4级']
for row in source_sheet.iter_rows():
    sort4.append(row[0].value)


sort5 = []
source_sheet = source_excel['5级']
for row in source_sheet.iter_rows():
    sort5.append(row[0].value)

source_sheet = source_excel['主表']

data = []
for row in source_sheet.iter_rows(min_row=2):
    data_dict ={}
    data_dict['p_n'] = row[2].value
    h_4 = row[15].value
    h_4_arr = h_4.split(',')
    for i in range(0,len(h_4_arr)):
        data_dict[f'sort{i+1}']=h_4_arr[i]
    if 'sort3' not in data_dict:
        data_dict['sort3'] = ''
    data_dict['sort4'] = row[11].value
    data_dict['sort5'] = row[24].value
    data.append(data_dict)

source_excel.close()
doc = Document()
datas_dict = {}
s5_num = 1
s1_num = 1
for s1 in sort1:
    print(s1)
    arr1 = list(filter(lambda item: item['sort1'] == s1, data))
    add_paragraph_heading1(docx=doc, text=f'{get_china_number(s1_num)}、{s1}（{len(arr1)}个）')
    s1_num += 1
    s2_num = 1
    for s2 in sort2:
        arr2 = list(filter(lambda item: item['sort2'] == s2, arr1))
        if len(arr2) > 0:
            add_paragraph_heading2(docx=doc, text=f'（{get_china_number(s2_num)}）{s2}（{len(arr2)}个）')
            s2_num += 1
            s3_num = 1
            if arr2[0]['sort3'] == '':
                s4_num = 1
                for s4 in sort4:
                    arr4 = list(filter(lambda item: item['sort4'] == s4, arr2))
                    add_paragraph_heading4(docx=doc, text=f'【{s4}】（{len(arr4)}个）')
                    s4_num += 1
                    for s5 in sort5 :
                        arr5 = list(filter(lambda item: item['sort5'] == s5, arr4))
                        if len(arr5) > 0:
                            for data_dict in arr5:
                                add_paragraph_heading5(docx=doc, text=f'{s5_num}.{data_dict["p_n"]}')
                                s5_num += 1



            else:
                for s3 in sort3:
                    arr3 = list(filter(lambda item: item['sort3'] == s3, arr2))
                    if len(arr3) > 0:
                        add_paragraph_heading3(docx=doc, text=f'{get_roman_number(s3_num)}.{s3}（{len(arr3)}个）')
                        s3_num += 1
                        s4_num = 1
                        for s4 in sort4:
                            arr4 = list(filter(lambda item: item['sort4'] == s4, arr3))
                            if len(arr4) > 0:
                                add_paragraph_heading4(docx=doc, text=f'【{s4}】（{len(arr4)}个）')
                                s4_num += 1
                                for s5 in sort5:
                                    arr5 = list(filter(lambda item: item['sort5'] == s5, arr4))
                                    if len(arr5) > 0:
                                        for data_dict in arr5:
                                            add_paragraph_heading5(docx=doc, text=f'{s5_num}.{data_dict["p_n"]}')
                                            s5_num += 1





print('----》结束《---')
doc.save(r'D:\Desktop\word\a.docx')